package com.bds.tjj.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bds.tjj.model.QuarterData;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.QuarterSumService;
import com.bds.tjj.utils.Const;

/**
 * Created by guoyu on 2018/1/29.
 */
@Controller
public class QuarterDetailController extends BaseAction {
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
            .getLogger(QuarterDetailController.class);

    @Autowired

    private QuarterSumService quarterSumService;

    @RequestMapping(value = "/data/quar/detail/index/{tt}", method = RequestMethod.GET)
    public String getMonthNum(Map<String, Object> map, @PathVariable String tt) throws IOException {

        if (StringUtils.isEmpty(tt)) {
            map.put("map", "");
        } else {
            map.put("map", tt);
        }

        return "ftl/analytics/quar_detail_data";
    }

    @RequestMapping(value = "/data/quar/detail/get/{tt}/{start}/{end}", method = RequestMethod.GET)
    @ResponseBody
    public Result getMonthSum(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable String tt,
                              @PathVariable String start, @PathVariable String end) throws IOException {
        Result ret = new Result();
        Integer start_month = 0;
        Integer end_month = 0;
        try {
            String startPre = start.substring(0, 4);
            String startBack = start.substring(4, 6);
            if (startBack.startsWith("0")) {
                startBack = startBack.replaceAll("0", "");
            }
            int tmpNum = Integer.parseInt(startBack);
            if (tmpNum >= 1 && tmpNum <= 3) {
                start = startPre + "01";
            } else if (tmpNum >= 4 && tmpNum <= 6) {
                start = startPre + "04";
            } else if (tmpNum >= 7 && tmpNum <= 9) {
                start = startPre + "07";
            } else {
                start = startPre + "10";
            }
            start_month = Integer.parseInt(start);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, "开始月份格式不对");
            return ret;
        }
        try {
            end_month = Integer.parseInt(end);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, "结束月份格式不对");
            return ret;
        }
        try {
            List<QuarterData> list = quarterSumService.getQuarterTrend(tt, start_month, end_month);

            ret.setResult(Const.SUC, list);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, null);
            LOGGER.warn("查询get_month_trend异常：", e);
        }
        return ret;
    }

}
